﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.CharacterDevelopment.HeroTraitDeveloper
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.Core;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.CharacterDevelopment
{
  public class HeroTraitDeveloper : PropertyOwner<PropertyObject>
  {
    internal static void AutoGeneratedStaticCollectObjectsHeroTraitDeveloper(
      object o,
      List<object> collectedObjects)
    {
      ((MBObjectBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Hero);
    }

    internal static object AutoGeneratedGetMemberValueHero(object o)
    {
      return (object) ((HeroTraitDeveloper) o).Hero;
    }

    [SaveableProperty(0)]
    internal Hero Hero { get; private set; }

    internal HeroTraitDeveloper(Hero hero)
    {
      this.Hero = hero;
      this.UpdateTraitXPAccordingToTraitLevels();
    }

    public void AddTraitXp(TraitObject trait, int xpAmount)
    {
      xpAmount += this.GetPropertyValue((PropertyObject) trait);
      int traitLevel;
      int traitXp;
      Campaign.Current.Models.CharacterDevelopmentModel.GetTraitLevelForTraitXp(this.Hero, trait, xpAmount, out traitLevel, out traitXp);
      this.SetPropertyValue((PropertyObject) trait, traitXp);
      if (traitLevel == this.Hero.GetTraitLevel(trait))
        return;
      this.Hero.SetTraitLevel(trait, traitLevel);
    }

    public void UpdateTraitXPAccordingToTraitLevels()
    {
      foreach (TraitObject traitObject in (List<TraitObject>) TraitObject.All)
      {
        int traitLevel = this.Hero.GetTraitLevel(traitObject);
        if (traitLevel != 0)
        {
          int requiredForTraitLevel = Campaign.Current.Models.CharacterDevelopmentModel.GetTraitXpRequiredForTraitLevel(traitObject, traitLevel);
          this.SetPropertyValue((PropertyObject) traitObject, requiredForTraitLevel);
        }
      }
    }
  }
}
